;(function() {
	'use strict';
	angular.module('controllers.sysServiceData.pBossSuccessRate')
		.controller('pBossSuccessRateController', pBossSuccessRateController);

	function pBossSuccessRateController($scope, pBossSuccessRate, COREDATA, odOperationDevelop, CoreService) {
		//控制查询条件拉上拉下   1为拉下  0 为拉上
		$scope.shoumun = {
			num: 1
		};
		//控制等待样式   false 为隐藏   true 为显示
		$scope._loading.show = false;
		$scope.provinceObj = pBossSuccessRate.getProvinceObj();
		$scope.sendData = pBossSuccessRate.getPageBind();
		$scope.timeTypes = pBossSuccessRate.getTimeType();
		$scope.formPrefix = COREDATA.FORMPREFIX + 'sysServiceData/pboss/pbossExcelExport';
		var year = new Date().getFullYear();
		var month = new Date().getMonth();
		$scope.$watch('sendData.timeType', function(newValue) {
			if (newValue === '3') {
				$scope.sendData.startTime = new Date(year, month);
				$scope.sendData.endTime = new Date(year, month);
			} else {
				$scope.sendData.startTime = Number(year);
				$scope.sendData.endTime = Number(year);
			}
		});
		$scope.$watch('sendData.provinceObj', function(code) {
			$scope.sendData.province = [];
			code === '1' ? $scope.provinces = COREDATA.PROVINCES : $scope.provinces = [{
				provinceCode: '1',
				provinceName: '暂无'
			}];
		});
		var myEcharts = echarts.init(document.getElementById('doc'));
		$scope.search = function() {
			if ($scope.shoumun.num === 1) {
				$scope.shoumun.num = 1;
				return;
			}
			if ($scope.sendData.provinceObj === '2') {
				$scope.shoumun.num = 1;
				CoreService.dialog('暂无数据(待调查)');
				return;
			}
			$scope.send = {};
			$scope.send.timeType = $scope.sendData.timeType;
			if ($scope.sendData.timeType === '3') {
				$scope.send.startTime = CoreService.dateFormat($scope.sendData.startTime, 'yyyy-MM');
				$scope.send.endTime = CoreService.dateFormat($scope.sendData.endTime, 'yyyy-MM');
			} else {
				$scope.send.startTime = $scope.sendData.startTime + '';
				$scope.send.endTime = $scope.sendData.endTime + '';
			}
			if ($scope.send.startTime === $scope.send.endTime) {
				$scope.title = $scope.send.startTime.replace(/-/g, '') + 'PBOSS业务交易成功率';
			} else {
				$scope.title = $scope.send.startTime.replace(/-/g, '') + '_' + $scope.send.endTime.replace(/-/g, '') + 'PBOSS业务交易成功率';
			}
			$scope.send.provinceNames = [];
			if ($scope.sendData.province.length === 0) {
				$scope.send.provinceNames = $scope.provinces.map(function(res) {
					return res.provinceName;
				});
			} else {
				$scope.send.provinceNames = $scope.sendData.province.map(function(res) {
					return res.provinceName;
				});
			}
			$scope.send.title = $scope.title;
			pBossSuccessRate.querySuccessRate($scope.send, function(res) {
				if (res.state === '10000' && res.data.length !== 0) {
					myEcharts.clear();
					var myOption = odOperationDevelop.getEchartsData(res.data, $scope.title, 'prov', 'time', 'successRate', 'bar');
					myOption.xAxis[0].axisLabel = {
						show: true,
						rotate: 52,
						textStyle: {
							color: 'black',
							fontWeight: 'bold'
						}
					};
					myOption.dataZoom = {
						show: false
					};
					myOption.yAxis[0] = {
						type: 'value',
						min: 90,
						max: 100,
						axisLabel: {
							show: true,
							formatter: '{value}%', // Template formatter!
						}
					};
					myOption.toolbox = {
						show: true,
						feature: {
							magicType: {
								show: true,
								type: ['line', 'bar']
							}
						}
					};
					myEcharts.setOption(myOption);
					$scope.flag = 'view';
				} else if (res.data.length === 0) {
					$scope.shoumun.num = 1;
					CoreService.dialog('查询无数据');
				} else {
					CoreService.dialog('查询错误');
				}
			});
			$scope.times = [];
			pBossSuccessRate.queryDet($scope.send, function(res) {
				if (res.state === '10000' && res.data.length !== 0) {
					$scope.items = res.data;
					angular.forEach($scope.items[0].detail, function(det) {
						if ($scope.times.indexOf(det.time) === -1) {
							$scope.times.push(det.time);
						}
					});
					exportView();
				} else if (res.data.length === 0) {
					$scope.shoumun.num = 1;
					CoreService.dialog('查询无数据');
				} else {
					$scope.shoumun.num = 1;
					CoreService.dialog('查询错误');
				}
			});
		};
		$scope.showView = function() {
			$scope.flag = 'view';
		};
		$scope.showTable = function() {
			$scope.flag = 'table';
		};
		//导出excel
		function exportView() {
			$scope.outData = JSON.stringify($scope.send);
		}
		$scope.sendDown = {
			page: {
				currentPage: 1,
				pageSize: '10'
			}
		};
		$scope.tableDown = function() {
			$scope.sendDown.startTime = $scope.send.startTime;
			$scope.sendDown.endTime = $scope.send.endTime;
			$scope.sendDown.timeType = $scope.send.timeType;
			// $scope.sendDown.provinceName = item.prov;
			$scope.sendDown.page = {
				currentPage: 1,
				pageSize: '10'
			};
			$scope.callback();
		};
		$scope.callback = function() {
			pBossSuccessRate.queryTableDet($scope.sendDown, function(res) {
				if (res.state === '10000') {
					$scope.totalCount = res.data.totalCount;
					$scope.downData = res.data.result;
					$scope.flag = 'tableDown';
				} else {
					CoreService.dialog('查询错误');
				}
			});
		};
		$scope.back = function() {
			$scope.flag = 'table';
		};
	}
	pBossSuccessRateController.$inject = ['$scope', 'pBossSuccessRate', 'COREDATA', 'odOperationDevelop', 'CoreService'];
})();